package Chinese.Education.Identify.service;

import Chinese.Education.Identify.mapper.TeacherMapper;
import Chinese.Education.Identify.pojo.Teacher;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TeacherService {
    @Resource
    private TeacherMapper teacherMapper;
    public Map<String, Object> selectTeacher(int page, int limit, String teacherId, String teacherName){
        int offset = (page - 1) * limit;
        List<Teacher> teachers = teacherMapper.selectTeacher(offset, limit,teacherId, teacherName);
        int count = teacherMapper.selectCount();
        Map<String, Object> teacher = new HashMap<>();
        teacher.put("code", 0);
        teacher.put("msg", "");
        teacher.put("count", count);
        teacher.put("data", teachers);
        return teacher;
    }

    public Map<String, Object> insertTeacher(Teacher teacher){
        Teacher teacher1 = teacherMapper.selectTeacherById(teacher.getTeacherId());
        if (teacher1 != null) {
            Map<String, Object> result = new HashMap<>();
            result.put("code", 500);
            result.put("msg", "添加失败，教师id已存在");
            return result;
        }
        Map<String, Object> result = new HashMap<>();
        teacherMapper.insertTeacher(teacher);
        result.put("code", 200);
        result.put("msg", "添加成功");
        result.put("data", teacher);
        return result;
    }
    public Map<String, Object> deleteTeacher(Teacher teacher){
        Map<String, Object> result = new HashMap<>();
        teacherMapper.deleteTeacher(teacher);
        result.put("code", 200);
        result.put("msg", "删除成功");
        result.put("data", teacher);
        return result;
    }

    public Map<String, Object> updateTeacher(Teacher teacher){
        Map<String, Object> result = new HashMap<>();
        teacherMapper.updateTeacher(teacher);
        result.put("code", 200);
        result.put("msg", "修改成功");
        result.put("data", teacher);
        return result;
    }

    public void updateDescription(String teacherId, String description) {
        teacherMapper.updateDescription(teacherId, description);
    }

}
